package servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.util.TreeMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Alberto
 */
public class FrontController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private TreeMap resources;


    @Override
    public void init(){
        java.util.Date date = new java.util.Date();
        resources = new TreeMap();
        resources.put("login", "index.jsp");
        resources.put("P1", "principal.jsp?month="+date.getMonth());
        resources.put("FM", "facturaMovil.jsp");
        resources.put("NFA", "nuevaFacturaAgua.jsp");
        resources.put("NFE", "nuevaFacturaElectricidad.jsp");
        resources.put("NFH", "nuevaFacturaHipoteca.jsp");
        resources.put("NFS", "nuevaFacturaSeguro.jsp");
        resources.put("NC", "nuevaCuenta.jsp");
        resources.put("NG", "nuevoGasto.jsp");
        resources.put("NN", "nuevaNota.jsp");
        resources.put("NT", "nuevaTarea.jsp");
        resources.put("NI", "nuevoIngreso.jsp");
        resources.put("NCT", "nuevoContTelefono.jsp");
        resources.put("NCL", "nuevoContLab.jsp");
        resources.put("C", "cuenta.jsp?month="+date.getMonth());
        resources.put("T", "telefonia.jsp");
        resources.put("E", "electricidad.jsp");
        resources.put("H", "hipoteca.jsp");
        resources.put("S", "seguro.jsp");
        resources.put("A", "agua.jsp");
        resources.put("V", "vehiculo.jsp");
        resources.put("MBCU", "modBorCuenta.jsp");
        resources.put("VT", "verTelefono.jsp");
        resources.put("FM", "facturaMovil.jsp");
        resources.put("MBCT", "modBorContratoTelefonia.jsp");
        resources.put("NCE", "nuevoContElectricidad.jsp");
        resources.put("NFE", "nuevaFacturaElectricidad.jsp");
        resources.put("MBC", "modBorContrato.jsp");
        resources.put("CP", "calcularPotencia.jsp");
        resources.put("CP1", "calculoPotencia.jsp");
        resources.put("NCH", "nuevoContHipoteca.jsp");
        resources.put("NFH", "nuevaFacturaHipoteca.jsp");
        resources.put("MBCH", "modBorContratoHipoteca.jsp");
        resources.put("CH", "calcularHipotecas.jsp");
        resources.put("CH1", "calculoHipoteca.jsp");
        resources.put("NCS", "nuevoContSeguro.jsp");
        resources.put("NFS", "nuevaFacturaSeguro.jsp");
        resources.put("MBCS", "modBorContratoSeguro.jsp");
        resources.put("NCA", "nuevoContAgua.jsp");
        resources.put("NFA", "nuevaFacturaAgua.jsp");
        resources.put("MBCA", "modBorContratoAgua.jsp");
        resources.put("VV", "verVehiculo.jsp");
        resources.put("NV", "nuevoVehiculo.jsp");
        resources.put("MBV", "modBorVehiculo.jsp");
        resources.put("MC", "modificarComponente.jsp");
        resources.put("PU", "perfilUsuario.jsp");
        resources.put("NS", "nuevoSalario.jsp");
        resources.put("MBCL", "modBorContratoLaboral.jsp");
    }


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String resource = request.getParameter("redir");
        if(resource.equals("login")){
            request.getSession().setAttribute("user", null);
            request.getSession().invalidate();
        }
        RequestDispatcher d = request.getRequestDispatcher((String)resources.get(resource));
        d.forward(request, response);
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
